/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xuidemo.slice.tabbar;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.PageSlider;

import com.xuexiang.xui_lib.tabbar.EasyIndicator;
import com.xuexiang.xui_lib.tabbar.tablayout.ContentPage;
import com.xuexiang.xuidemo.ResourceTable;
import com.xuexiang.xuidemo.slice.tabbar.provider.EasyIndicatorProvider;

import java.util.ArrayList;

/**
 * EasyIndicatorFragmentSlice
 *
 * @since 2021-04-19
 */
public class EasyIndicatorFragmentSlice extends AbilitySlice {
    EasyIndicator mEasyIndicator;
    PageSlider mPageSlider;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_slice_easyindicator_page);
        mEasyIndicator = (EasyIndicator) findComponentById(ResourceTable.Id_easy_indicator);
        mPageSlider = (PageSlider) findComponentById(ResourceTable.Id_page_slider);

        initIndicatorViewPager();
    }

    private void initIndicatorViewPager() {
        mEasyIndicator.setTabTitles(ContentPage.getPageNames());
        mEasyIndicator.setPageSlider(mPageSlider);
        mPageSlider.setPageCacheSize(ContentPage.size() - 1);
        if (mPageSlider.getProvider() == null) {
            EasyIndicatorProvider easyIndicatorProvider = new EasyIndicatorProvider(getData());
            mPageSlider.setProvider(easyIndicatorProvider);
        }
        mPageSlider.setCurrentPage(0);
    }

    private ArrayList<EasyIndicatorProvider.DataItem> getData() {
        ArrayList<EasyIndicatorProvider.DataItem> dataItems = new ArrayList<>();

        String[] arr = ContentPage.getPageNames();
        for (int index = 0; index < arr.length; index++) {
            dataItems.add(new EasyIndicatorProvider.DataItem(String.format("这个是%s页面的内容",
                arr[index])));
        }
        return dataItems;
    }
}